package com.hrbxlh.support.web.auth.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.hrbxlh.support.web.auth.po.LoginPO;
import com.hrbxlh.support.web.auth.service.AuthService;
import com.hrbxlh.support.web.auth.vo.DateVO;
import com.hrbxlh.support.web.auth.vo.WXSign;
import com.hrbxlh.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;

/**
 * @author lx
 */
@RestController
@RequestMapping("/unauthorized/auth")
@Slf4j
public class AuthController {

    @Autowired
    private AuthService authService;

    @GetMapping("/mobile")
    public Result<Integer> authMobile(@RequestParam String code, HttpSession session) {
        return Result.success(authService.authMobile(code, session));
    }

    @PostMapping("/login")
    public Result<Integer> login(@RequestBody @Valid LoginPO loginPO, HttpSession session) {
        return Result.success(authService.login(loginPO, session));
    }

    @GetMapping("/date")
    public Result<DateVO> getDate(
            @RequestParam(required = false, defaultValue = DatePattern.NORM_DATE_PATTERN) String formate) {
        DateVO dateVO = DateVO.builder()
                .date(DateUtil.format(new Date(), formate))
                .week(DateUtil.dayOfWeekEnum(new Date()).toChinese())
                .currentTime(new Date().getTime())
                .build();
        return Result.success(dateVO);
    }

    @GetMapping("/sign")
    public Result<WXSign> getWXSign(@RequestParam String url) {
        WXSign wxSign = authService.getWXSign(url);
        return Result.success(wxSign);
    }

}
